package com.sgl.controller;

import com.sgl.entity.Dict;
import com.sgl.entity.OrderHistory;
import com.sgl.service.DictService;
import com.sgl.util.PageBean;
import com.sgl.util.ReturnData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 字典表管理
 */
@RestController
@RequestMapping("/sgl/dict")
public class BgDictController {
    @Autowired
    DictService dictService;

    @RequestMapping("/findList")
    public ReturnData findList(PageBean<Dict> pageBean, String decribe, String key){
        HashMap<String, Object> param = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(decribe)) {
            param.put("decribe", decribe);
        }
        if(StringUtils.isNotBlank(key)) {
            param.put("key", key);
        }

        pageBean.setParam(param);
        List<Dict> list = dictService.findList(pageBean);
        int count = dictService.countList(pageBean);
        pageBean.setContent(list);
        pageBean.setTotalCount(count);
        return new ReturnData().okBack(pageBean);
    }

    @PostMapping("/add")
    public ReturnData add(Dict dict){
        ReturnData rd = new ReturnData();
        Dict dict1=dictService.findByKey(dict.getKey());
        if (dict1==null){
            int count = dictService.insert(dict);
            if(count > 0) {
                return rd.okBack();
            }
            return rd.failBack();
        }else{
            return rd.failBack("当前key已存在无法新增！");
        }


    }

    @PostMapping("/update")
    public ReturnData update(Dict dict){
            ReturnData rd = new ReturnData();
        int count = dictService.update(dict);
        if(count > 0) {
            return rd.okBack();
        }
        return rd.failBack();
    }

//    @RequestMapping("/delete")
//    public ReturnData delete(String ids) {
//        ReturnData rd = new ReturnData();
//        if(StringUtils.isBlank(ids)) {
//            return rd.failBack("请选择要删除的内容");
//        }
//        int rs =dictService.delete(Arrays.asList(ids.split(",")));
//        if (rs==0){
//            return rd.failBack("没有删除任何内容");
//        }
//        return rd.okBack("共删除"+rs+"条记录");
//    }
}
